package net.ustc.javamemcached.backup;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;

import net.spy.memcached.MemcachedClient;
import net.ustc.javamemcached.memorystore.MemoryEntry;
import net.ustc.javamemcached.protocol.ProtocolMessage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class BackupStoreManagerImpl implements IBackupStoreManager {
	final Logger log = LoggerFactory.getLogger(BackupStoreManagerImpl.class);
	private boolean enableSync = false;
	private boolean enableAsync = true;
	private MemcachedClient mc;
	
	/**
	 * used to keep all K/V need to be backup. a thread will shedule to remove
	 * element from this queue to send it to backup cache server
	 */
	private ConcurrentLinkedQueue<MemoryEntry> backupQueue = new ConcurrentLinkedQueue();

	
	class BackupElement{
		int backupNum;
		ProtocolMessage message;
		public BackupElement(int backupNum, ProtocolMessage message){
			this.backupNum = backupNum;
			this.message = message;
		}
	}
	
	
	public BackupStoreManagerImpl(List<InetSocketAddress> servers){
        try {
			mc = new MemcachedClient(servers);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void init() {
		// TODO load server info
		
	}
	
	public void asyncSend(int backupNum, ProtocolMessage message) {
		// TODO Auto-generated method stub
		BackupElement be = new BackupElement(backupNum, message);
		sendQueue.offer(be);
	}

	public boolean send(int backupNum, ProtocolMessage message) {
		// TODO Auto-generated method stub
		return false;
	}

	public void close() {
		// TODO Auto-generated method stub

	}

	public void handleBackupQueue(){
		if(enableAsync){
			while(BackupElement e = (BackupElement)backupQueue.remove() != null){
				
			}
		}
	}

}
